package com.bd.rj.backstage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bd.rj.backstage.mapper.RoleMapper;
import com.bd.rj.backstage.mapper.UserMapper;
import com.bd.rj.backstage.service.IUserService;
import com.bd.rj.common.entity.User;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 白世俊
 * @since 2025-01-03
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
    @Resource
    UserMapper userMapper;
    @Resource
    RoleMapper roleMapper;

    @Override
    public IPage<User> getUserByPage(int pageNum, QueryWrapper<User> queryWrapper) {
        Page<User> page = new Page(pageNum, 10);
        page = this.userMapper.selectPage(page, queryWrapper);
        return page;
    }

    @Override
    public String deleteUser(Long[] id) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(User::getId,id);
        boolean result = this.remove(queryWrapper);
        return result?"成功":"失败";
    }

    @Override
    public IPage<Map<String, Object>> queryrole(IPage<Map<String, Object>> page) {
        return userMapper.queryrole(page);
    }

    @Override
    public IPage<Map<String, Object>> queryrolename(IPage<Map<String, Object>> page, String rname) {
        return userMapper.queryrolename(page, rname);
    }

    @Override
    public IPage<Map<String, Object>> querystatus(IPage<Map<String, Object>> page, Integer status) {
        return userMapper.querystatus(page, status);
    }

    @Override
    public IPage<Map<String, Object>> queryuname(IPage<Map<String, Object>> page, String username) {
        return userMapper.queryuname(page, username);
    }


    @Override
    public Map<String, Object> getUserDataForExport(Long[] id) {
        // 根据前端传入的id，使用MyBatis Plus构建查询条件并查询对应的用户记录
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(User::getId, id);
        List<User> records = list(queryWrapper);
        // 获取表头映射
        LinkedHashMap<String, String> titleMap = getTileMap();

        // 准备用于返回的数据列表，每个元素对应一行数据（用数组表示，方便后续填充Excel）
        List<String[]> dataList = new ArrayList<>();
        for (User u : records) {
            String[] rowData = {
                    u.getUsername(),
                    u.getRealname(),
                    u.getPhone(),
                    u.getEmail(),
                    u.getProfession(),
                    u.getCreatetime().toString(),
                    u.getSex()
            };
            dataList.add(rowData);
        }
        Map<String, Object> result = new HashMap<>();
        result.put("titleMap", titleMap);
        result.put("dataList", dataList);
        return result;
    }

    private static LinkedHashMap<String, String> getTileMap() {
        LinkedHashMap<String, String> names = new LinkedHashMap<>();
        names.put("username", "用户名");
        names.put("realname", "真实姓名");
        names.put("phone", "联系方式");
        names.put("email", "电子邮件");
        names.put("profession", "岗位");
        names.put("createtime", "创建时间");
        names.put("sex", "性别");
        return names;
    }

    @Override
    public Map<String, Object> getLogin(String username,String password) {
        Map<String, Object> loginInfo = userMapper.getLogin(username,password);
        if (loginInfo == null) {
            return loginInfo;
        }
        List<Map<String, Object>> menuInfo = userMapper.getRoleUrls((Long) loginInfo.get("rid"));
        System.out.println(menuInfo);
        loginInfo.put("item",menuInfo);
        return loginInfo;
    }

    @Override
    public String getUserRoles(Long id) {
        return userMapper.getUserRoles(id);
    }

    @Override
    public IPage<Map> getEnterpriseByConditionAndPage(int pagenum, int pageSize, Map<String, Object> map) {
        IPage<Map> page = new Page<>(pagenum,pageSize);
        return userMapper.query(page,map);
    }

    @Override
    public void insertUserRole(Long userId) {
        userMapper.insertUserRole(userId);
    }

    @Override
    public void batchImportUsers(Long userId) {
        userMapper.batchInsertUserRoles(userId);
    }

//    @Override
//    public IPage<Map<String, Object>> queryroleExcludeRole1(IPage<Map<String, Object>> page) {
//        return userMapper.queryroleExcludeRole1(page);
//    }



}
